package cn.stylefeng.guns.orderitem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.order.entity.Order;
import cn.stylefeng.guns.orderitem.dto.OrderItemDTO;
import cn.stylefeng.guns.orderitem.entity.OrderItem;
import cn.stylefeng.guns.orderitem.mapper.OrderItemMapper;
import cn.stylefeng.guns.orderitem.service.OrderItemService;
import cn.stylefeng.guns.orderitem.enums.OrderItemExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * order_item 服务实现类
 *
 * @author aria
 * @date 2021/02/20 20:24
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {

    @Override
    public PageResult<OrderItem> page(OrderItemDTO orderItemDTO) {

        // 构造条件
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();

        // 拼接查询条件-
        if (orderItemDTO != null && ObjectUtil.isNotEmpty(orderItemDTO.getItemId())) {
            queryWrapper.like(OrderItem::getItemId, orderItemDTO.getItemId());
        }

        // 拼接查询条件-
        if (orderItemDTO != null && ObjectUtil.isNotEmpty(orderItemDTO.getOrderId())) {
            queryWrapper.like(OrderItem::getOrderId, orderItemDTO.getOrderId());
        }

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderItem> list(OrderItemDTO orderItemDTO) {

        // 构造条件
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderItemDTO orderItemDTO) {

        // 将dto转为实体
        OrderItem orderItem = new OrderItem();
        BeanUtil.copyProperties(orderItemDTO, orderItem);

        this.save(orderItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderItemDTO orderItemDTO) {
        this.removeById(orderItemDTO.getItemId());
    }

    @Override
    public void edit(OrderItemDTO orderItemDTO) {

        // 根据id查询实体
        OrderItem orderItem = this.queryOrderItem(orderItemDTO);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderItemDTO, orderItem);

        this.updateById(orderItem);
    }

    @Override
    public OrderItem detail(OrderItemDTO orderItemDTO) {
        return this.queryOrderItem(orderItemDTO);
    }

    @Override
    public List<OrderItem> getItemsByOrderId(Order order) {
        Integer orderId = order.getOrderId();
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return baseMapper.selectList(queryWrapper);
    }


    /**
     * 获取order_item
     *
     * @author aria
     * @since 2021/02/20 20:24
     */
    private OrderItem queryOrderItem(OrderItemDTO orderItemDTO) {
        OrderItem orderItem = this.getById(orderItemDTO.getItemId());
        if (ObjectUtil.isEmpty(orderItem)) {
            throw new ServiceException(OrderItemExceptionEnum.NOT_EXISTED);
        }
        return orderItem;
    }
}